package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.DBHelper;
import beans.Employer;
import beans.JobBean;

/**
 * Servlet implementation class EmployerMessage
 */
@WebServlet("/EmployerMessage")
public class EmployerMessage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmployerMessage() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    protected void handEmployerMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
    	
    	HttpSession session=request.getSession();
    	
    	String employerUsername= session.getAttribute("user").toString();
    	Connection con = DBHelper.getConnection();
		PreparedStatement sql = DBHelper.getprepareStatement(con,"select applyjob.employeeusername,employee.name,employee.phonenumber,employee.mail,employee.INTRODUCTION,employee.education from employee,applyjob where employee.EMPLOYEEUSERNAME = applyjob.EMPLOYEEUSERNAME and applyjob.EMPLOYERUSERNAME = ?");
		sql.setString(1, employerUsername);
    	
		ResultSet rs = sql.executeQuery();
    	
		ArrayList<Employer> employerMessages=new ArrayList<Employer>();
		
    	while(rs.next()){
    		Employer employerMessage = new Employer();

    		employerMessage.setEmployeeUsername(rs.getString("employeeusername"));
    		
    		employerMessage.setName(rs.getString("name"));
    		employerMessage.setPhonenumber(rs.getString("phonenumber"));
    		employerMessage.setMail(rs.getString("mail"));
    		employerMessage.setIntroduction(rs.getString("INTRODUCTION"));
    		employerMessage.setEducation(rs.getString("education"));
    		
    		employerMessages.add(employerMessage);
    		
    	}
    	request.setAttribute("employerMessages", employerMessages);
    	
    	request.getRequestDispatcher("employerMessage.jsp").forward(request, response);
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			handEmployerMessage(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			handEmployerMessage(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
